window.onerror = function(){return true;};
nehnre.loading = 3;
nehnre.selected = {};
$(function(){
	$( "#dialog-modal" ).dialog({
		height: 85,
		resizable: false,
		closeOnEscape: false,
		modal: true,
		dialogClass: 'hidden'
	});
	$( "#personal_edit" ).dialog({
		height: 440,
		width:350,
		resizable: false,
		modal: true,
		autoOpen:false
	});	
	loadUserInfo();


	$("#personal_btn").children().each(function(_index) {
		$(this).children().eq(0).mouseover(function(){
			btnMouseOver.call(this,_index);
		}).mouseout(function(){
			btnMouseOut.call(this,_index);
		}).children().eq(1).click(function(){
			delFavorites.call(this,_index);
		}).css("cursor", "pointer").attr("title", "删除").children().eq(0).attr("src","./images/close.gif");
		$(this).children().eq(0).children().eq(0).click(function(){
			favoritesClick.call(this,_index);
		});
	});
	setTimeout(function(){
		if($("#dialog-modal").dialog("isOpen") && $("#error_info").css("display")!="block"){
//			if($("#user_info").find("img").attr("src").indexOf("loading") > 0){
//				$("#user_info").find("img").attr("src","./images/error.gif");
//			}
//			if($("#personal_fav").find("img").attr("src").indexOf("loading") > 0){
//				$("#personal_fav").find("img").attr("src","./images/error.gif");
//			}
//			if($("#message_info").find("img").attr("src").indexOf("loading") > 0){
//				$("#message_info").find("img").attr("src","./images/error.gif");
//			}
			$("#error_info").children().eq(0).html("网络缓慢？请尝试<a href='###' onclick='javascript:location.href=location.href'>刷新</a>或稍后再试！");
			$("#error_info").show();
		}
	},20000);

});
function closeWaiting(){
	nehnre.loading --;
	if(!nehnre.loading){
		setTimeout(function(){$( "#dialog-modal" ).dialog("close");},1000);
	}
}
function btnMouseOver(_index){
	if(!$(this).children().eq(0).attr("blank")){
		$(this).removeClass("mouse_out_window");
		$(this).addClass("mouse_over_window");
		$(this).children().eq(1).show();
	}
}
function btnMouseOut(_index){
	$(this).removeClass("mouse_over_window");
	$(this).addClass("mouse_out_window");
	$(this).children().eq(1).hide();
}
function delFavorites(_index){
	var img = $(this).prev();
	$(this).hide();
	$.ajax({
		url:"user_favorites_delete.jsp",
		type:"GET",
		cache:false,
		data:"usfaId=" + img.attr("usfa_id")
	});	
	var title = $("#fav_table").find("tr").eq(1).children().eq(_index);
	title.html("暂无收藏");
	img.attr("src","./images/icon_0.jpg");
	img.attr("blank","true");
}
function favoritesClick(_index){
	if($(this).attr("blank")){
		nehnre.selected.img=$(this);
		nehnre.selected.title= $("#fav_table").find("tr").eq(1).children().eq(_index);
		nehnre.selected.index = _index;
		$( "#personal_edit" ).dialog("open");
	} else {
		var _url = $(this).attr("_url");
		if(_url == "../index.html"){
			window.open(_url,"_blank","directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,width=750,height=500");
		} else {
			location.href = $(this).attr("_url");
		}
	}
}
function chooseClick(_index){
	var obj = this;
	$.ajax({
		url:"user_favorites_save.jsp",
		type:"GET",
		cache:false,
		data:"usfaNum=" + nehnre.selected.index + "&uflId=" + $(this).attr("ufl_id"),
		success:function(json){
			json = nehnre.parseJSON(json);
			if(json.success){
				fillAfterSave.call(obj, _index, json.usfaId);
			}
		}
	});
}
function loadPersonalFav(logName){
	if(!logName) return;
	$("#personal_fav").show();
	$.ajax({
		url:"user_favorites_load.jsp",
		type:"POST",
		cache:false,
		data:"logName=" + logName,
		success:function(json){
			$("#personal_fav").children().eq(1).attr("src","./images/ok.png");
			loadMessageInfo();
			
			json = nehnre.parseJSON(json);
			var ufArr = json.ufArr;
			for(var i=0;i<ufArr.length;i++){
				$("#personal_btn").children().eq(ufArr[i].usfaNum).find("img").eq(0).attr("src", ufArr[i].uflPicUrl).attr("blank","").attr("_url", ufArr[i].uflLinkUrl).attr("usfa_id", ufArr[i].usfaId);
				$("#personal_btn").next().children().eq(ufArr[i].usfaNum).html(ufArr[i].uflName);
			}
			fillChooseBtn(json);
		},
		error:function(){
			$("#personal_fav").find("img").attr("src","./images/error.gif");
			$("#error_info").children().eq(0).html("状态码：" + arguments[0].status + "，错误信息：" + arguments[2] + "！");
			$("#error_info").show();				
		},
		complete:function(){
			closeWaiting();
		}
	});
}
function loadUserInfo(){
	$("#user_info").show();
	$.getJSON("http://member.sgst.cn/jsp/user_info.jsp?jsoncallback=?", function(result){
		if(result.success){
			$("#inact").html(nehnre.decode(result.data.inact));
			$("#mark").html(result.data.mark);
			$("#user_info").children().eq(1).attr("src","./images/ok.png");
			loadPersonalFav(nehnre.decode(result.data.logName));
		} else if(result.errorType==1){
			$("#user_info").find("img").attr("src","./images/error.gif");
			$("#error_info").children().eq(0).html("未登录或登录超时，请重新登录！");
			$("#error_info").show();			
		}
		closeWaiting();
	});	
}
function loadMessageInfo(){
	$("#message_info").show();
	var _iframe = $("<iframe></iframe>");
	_iframe.hide().appendTo("body").attr("src","http://member.sgst.cn/WEBUI/shortMessage.do?method=listInbox");
	_iframe.load(function(){
		$.getJSON("http://member.sgst.cn/WEBUI/jsp/message_info.jsp?jsoncallback=?", function(result){
			if(result.success){
				for(var i=0;i<result.data.length;i++){
					var tr = $("#message").clone();
					tr.removeAttr("id");
					var sender = nehnre.decode(result.data[i].sender);
					var subject = result.data[i].readStatus == "new"?"<img src='./images/notread.gif' />":"<img src='./images/read.gif' />";
					subject += "&nbsp;<a href='http://member.sgst.cn/WEBUI/shortMessage.do?method=displayMessage&type=";
					subject += nehnre.decode(result.data[i].messageType);
					subject += "&id=" + nehnre.decode(result.data[i].messageId) + "'>";
					subject += result.data[i].readStatus == "new"?"<b>" + nehnre.decode(result.data[i].subject) + "</b>":nehnre.decode(result.data[i].subject);
					subject += "</a>";
					tr.children().eq(0).html(subject);
					tr.children().eq(1).html(sender?sender:"研发平台");
					tr.children().eq(2).html(nehnre.decode(result.data[i].createTime));
					$("#message").parent().append(tr);
					tr.show();
				}
				$("#message_info").children().eq(1).attr("src","./images/ok.png");
				closeWaiting();
			} else if(result.errorType == 1){
				$("#message_info").find("img").attr("src","./images/error.gif");
				$("#error_info").children().eq(0).html("消息列表授权失败，点击<a href='http://member.sgst.cn/WEBUI/jump.do?backurl="+nehnre.encode(location.href)+"'>这里</a>转向登录页面！");
				$("#error_info").show();
			} else if(result.errorType == 2){
				$("#message_info").find("img").attr("src","./images/error.gif");
				$("#error_info").children().eq(0).html("消息列表读取失败！");
				$("#error_info").show();	
				closeWaiting();
			}
			
		});		
	});

}
function fillChooseBtn(json){
	var uflArr = json.uflArr;
	var btnTr;
	var titleTr;
	for(var i=0;i<uflArr.length;i++){
		var n = i%3;
		if(!n){
			btnTr = $("#choose_btn_model").clone()
			titleTr = $("#choose_btn_model").next().clone()
			btnTr.removeAttr("id");
			titleTr.removeAttr("id");
		}
		btnTr.children().eq(n).find("img").attr("src", uflArr[i].uflPicUrl).attr("_url", uflArr[i].uflLinkUrl).attr("ufl_id", uflArr[i].uflId);
		titleTr.children().eq(n).html(uflArr[i].uflName);
		if(n==2){
			$("#choose_fav").append(btnTr);
			$("#choose_fav").append(titleTr);
			btnTr.show();
			titleTr.show();
		}
	}
	$("#choose_fav").find("img").each(function(_index){
		$(this).click(function(){
			chooseClick.call(this,_index);
		});
	});	
}
function fillAfterSave(_index, usfaId){
		var i = _index % 3;
		var title = $(this).parent().parent().parent().next().children().eq(i);
		nehnre.selected.img.attr("src", $(this).attr("src"));
		nehnre.selected.img.attr("blank","");
		nehnre.selected.img.attr("_url", $(this).attr("_url"));
		nehnre.selected.img.attr("usfa_id", usfaId);
		nehnre.selected.title.html(title.html());
		$( "#personal_edit" ).dialog("close");
}